package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.repository.*;
import com.hualude.roadsign.service.RoadLineFaultService;
import com.hualude.roadsign.utils.PushUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by pjn on 2017/8/14.
 */
@Service
public class RoadLineFaultServiceImpl implements RoadLineFaultService {
    @Resource
    private RoadLineFaultRepository roadLineFaultRepository;
    @Resource
    private RoadLineRepository roadLineRepository;
    @Resource
    private UserRepository userRepository;
    @Resource
    private OrgRepository orgRepository;
    @Resource
    private PushHistoryRepository pushHistoryRepository;

    private String strUserIds;

    private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public DTResponseDto getRoadLineReportFault4DT(DTRequestDto dtRequestDto, Integer actorId, Integer userId, Integer orgId) throws Exception {
        Integer count;
        List<DTLineFaultDto> dto4DTs;
        count = roadLineFaultRepository.getRoadLineReportFaultCount4DT(actorId, userId, orgId, dtRequestDto.getSearch());
        dto4DTs = roadLineFaultRepository.getRoadLineReportFault4DT(dtRequestDto, actorId, userId, orgId);
        for (DTLineFaultDto dto : dto4DTs) {
            dto.setRoadLineCheckerName(userRepository.getUserNameByUserId(dto.getRoadLineCheckerId()));
        }
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
        return dtResponseDto;
    }

    @Transactional
    public Integer updateRoadLineFault(Integer roadLineFaultId, Integer roadLineFaultLevel, Integer roadLineFaultType, String roadLineFaultPhoto, String roadLineFaultReason, Double roadLineMoney) throws Exception {
        return roadLineFaultRepository.updateRoadLineFault(roadLineFaultId, roadLineFaultLevel, roadLineFaultType, roadLineFaultPhoto, roadLineFaultReason, roadLineMoney);
    }

    @Transactional
    public Integer deleteRoadLineFaultByIds(String roadLineFaultIds) throws Exception {
        String[] faultIdArray = roadLineFaultIds.split(",");
        int i = 0;
        for (i = 0; i < faultIdArray.length; i++) {
            roadLineFaultRepository.deleteRoadLineFaultByIds(Integer.parseInt(faultIdArray[i]));
        }
        return i;
    }


    public Integer reportRoadLineFault(final Integer roadLineId, Integer roadLineFaultCreatorId, Integer roadLineFaultLevel, Integer roadLineFaultType,
                                       String roadLineFaultPhoto, String roadLineFaultReason, Double roadLineMoney, final Integer roadLineOrgId, Integer roadLineMainOrgId) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        if (roadLineMoney == null)
            roadLineMoney = 0.0;
        LineFaultDto insertDto = new LineFaultDto();
        insertDto.setRoadLineId(roadLineId);
        insertDto.setRoadLineFaultCreatorId(roadLineFaultCreatorId);
        insertDto.setRoadLineFaultLevel(roadLineFaultLevel);
        insertDto.setRoadLineFaultType(roadLineFaultType);
        insertDto.setRoadLineFaultTime(df.format(new Date()));
        insertDto.setRoadLineFaultPhoto(roadLineFaultPhoto);
        insertDto.setRoadLineFaultReason(roadLineFaultReason);
        insertDto.setRoadLineMoney(roadLineMoney);
        insertDto.setRoadLineOrgId(roadLineOrgId);
        insertDto.setRoadLineMainOrgId(roadLineMainOrgId);

        List<UserDto> userDtos = userRepository.getUserIdsByOrgIdAndActorId(roadLineOrgId, 3);
        final String msg = "标线 NO：" + roadLineId + " 待审核";
        strUserIds = "";
        for (UserDto userDto : userDtos) {
            strUserIds += userDto.getUserId().toString() + ",";
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    if (!strUserIds.equals("")) {
                        strUserIds = strUserIds.substring(0, strUserIds.length() - 1);
                        PushUtil.sendPushToUser(strUserIds, msg);
                        PushHistoryDto pushHistoryDto = new PushHistoryDto(roadLineOrgId, 3, msg, formatter.format(new Date()),6);
                        pushHistoryRepository.save(pushHistoryDto.toBean());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

        return roadLineFaultRepository.save(insertDto.toBean()).getRoadLineFaultId();
    }

    public Integer getRoadLineFaultIsReport(Integer roadLineId) throws Exception {
        List<LineFaultDto> lineFaultDtos = roadLineFaultRepository.getRoadLineFaultIsReport(roadLineId);
        Integer result = 1;
        if (lineFaultDtos.size() != 0) {
            Integer mainCheck = lineFaultDtos.get(0).getRoadLineMainCheck();
            Integer check = lineFaultDtos.get(0).getRoadLineCheck();
            Integer status = lineFaultDtos.get(0).getRoadLineMaintenanceStatus();
            if (status == 2) {
                result = 1;//维修完成
            } else if (mainCheck == -1 || check == -1) {
                result = 1;//
            } else {
                result = 0;
            }
        }
        return result;
    }

    public DTResponseDto getRoadLineFaultReport4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId, String roadName, String startRoad, String endRoad) throws Exception {
        if (roadName.equals("") && startRoad.equals("") && endRoad.equals("")) {
            return new DTResponseDto(dtRequestDto.getDraw(), 0, 0, "");
        } else {
            List<DTLineFaultDto> dto4DTs = roadLineRepository.getRoadLineFaultReport4DT(dtRequestDto, actorId, orgId, roadName, startRoad, endRoad);
            for (DTLineFaultDto dto : dto4DTs) {
                List<LineFaultDto> monitorFaultDtos = roadLineFaultRepository.getRoadLineFaultIsReport(dto.getRoadLineId());
                Integer result = 1;
                if (monitorFaultDtos.size() != 0) {
                    Integer mainCheck = monitorFaultDtos.get(0).getRoadLineMainCheck();
                    Integer check = monitorFaultDtos.get(0).getRoadLineCheck();
                    Integer status = monitorFaultDtos.get(0).getRoadLineMaintenanceStatus();
                    if (status == 2) {
                        result = 1;//维修完成
                    } else if (mainCheck == -1 || check == -1) {
                        result = 1;//
                    } else {
                        result = 0;
                    }
                }
                dto.setReportStatus(result);
            }
            DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), 1, 1, dto4DTs);
            return dtResponseDto;
        }
    }

    public List<String> getStartRoad(String roadName, Integer actorId, Integer orgId) throws Exception {
        return roadLineRepository.getRoadURoadName(roadName, actorId, orgId);
    }

    public List<String> getSectionByStartRoad(String roadName, Integer actorId, Integer orgId) throws Exception {
        return roadLineRepository.getSectionUStartRoad(roadName, actorId, orgId);
    }

    @Override
    public List<LineFaultDto> getRoadLineCoordinates4AppMap(Integer orgId, Integer orgType, Integer actorId) throws Exception {
        return roadLineRepository.getRoadLineCoordinates4AppMap(orgId, orgType, actorId);
    }
}
